/**
	 * 添加图文
	 * @returns
	 */
	function addNewArticle(){
		var row = 0;
		var tableMarginTop = 0;
		var count = 0;
		$("#articleModel tr[id]").each(function(i){
			var trid = this.id;
			trid = trid.substr(trid.indexOf("_")+1);
			if(parseInt(trid) >= row){
				row = parseInt(trid) + 1;
			}
			count++;
		});
		if(count < 10){
			//tableMarginTop = tableMarginTop + 80 * (count - 1);
			//模型html
			var modelHtml = "";
			//模型对应表单html
			var formHtml = "";
			modelHtml += '<tr id="article_' + row + '" style="height: 80px;position: relative;" >';
			modelHtml += '<td>';
			modelHtml += '<div class="article_item"  onmouseover="showEdit(\'article_edit_' + row + '\')" onmouseout="hideEdit(\'article_edit_' + row + '\')">';
			modelHtml += '<table width="100%">'
			modelHtml += '<tr>';
			modelHtml += '<td><div style="width: 172px;overflow: hidden;max-height: 42px;"><h2 id="article_title_' + row + '" class="title">标题</h2></div></td>'
			modelHtml += '<td width="30%">';
			modelHtml += '<div style="height: 78px;overflow: hidden;">';
			modelHtml += '<img id="article_img_' + row + '" src="" />';
			modelHtml += '<div id="article_thumb_' + row + '" class="thumb" >缩略图</div>';
			modelHtml += '</div>';
			modelHtml += '</td>';
			modelHtml += '</tr>';
			modelHtml += '</table>';
			modelHtml += '<div class="edit_item" id="article_edit_' + row + '">'
			modelHtml += '<a href="javascript:editArticle(' + row + ')">编辑</a>&nbsp;&nbsp;&nbsp;<a href="javascript:deleteArticle(' + row + ')">删除</a>';
			modelHtml += '</div>';
			modelHtml += '</div>';
			modelHtml += '</td>';
			modelHtml += '</tr>';
			$("#article_"+(row-1)).after(modelHtml);
			
			formHtml += '<table id="form_article_' + row + '" class="maintable" width="100%" style="margin-top: ' + tableMarginTop + 'px;display: none;>';
			formHtml += '<tr style="height: 30px;">';
			formHtml += '<td>标题:</td>';
			formHtml += '<td>';
			formHtml += '<input id="title_' + row + '" type="text" name="articles[' + row + '].title" onkeyup="showTitle(\'' + row + '\')"/> ';
			formHtml += '<input type="hidden" name="articles[' + row + '].order" value="' + row + '"/>'
			formHtml += '<span class="hint" id="hint_title_' + row + '"></span>';
			formHtml += '</td>';
			formHtml += '</tr>';
			formHtml += '<tr style="height: 30px;">';
			formHtml += '<td>描述:</td>';
			formHtml += '<td >';
			formHtml += '<textarea id="description_' + row + '" rows="2" cols="30" name="articles[' + row + '].description"></textarea>';
			formHtml += '<span class="hint" id="hint_description_' + row + '"></span>';
			formHtml += '</td> ';
			formHtml += '</tr>';
			formHtml += '<tr style="height: 30px;">';
			formHtml += '<td>封面:</td>';
			formHtml += '<td>'
			formHtml += '<input type="button" onclick="selectfile(\'file_' + row + '\')" class="ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false" value="上传"/>';
			formHtml += '<input id="pic_' + row + '" type="hidden" name="articles[' + row + '].picUrl"/>';
			formHtml += '<input type="file" id="file_' + row + '" name="fileToUpload" onchange="uploadPic(\'file_' + row + '\')" class="file"/>';
			formHtml += '<span class="hint" id="hint_cover_0"></span>';
			formHtml += '</td>';
			formHtml += '</tr>';
			formHtml += '<tr id="showCover_' + row + '" style="height: 80px;display: none;">';
			formHtml += '<td>&nbsp;</td>';
			formHtml += '<td>';
			formHtml += '<div id="showPic_' + row + '"  style="height: 80px;overflow: hidden;float: left;">';
			formHtml += '<img id="img_' + row + '" src="" width="80px" />';
			formHtml += '</div>';
			formHtml += '<a href="javascript:deletePic(\'pic_' + row + '\')" style="line-height: 80px;margin-left: 15px;">删除</a>';
			formHtml += '</td>';
			formHtml += '</tr>';
			formHtml += '<tr style="height: 30px;">';
			formHtml += '<td>连接地址:</td>';
			formHtml += '<td><input id="url_' + row + '" type="text" name="articles[' + row + '].url"/><span class="hint" id="hint_url_' + row + '"></span></td>';
			formHtml += '</tr>';
			formHtml += '</table>';
			$("#form_article_"+(row-1)).after(formHtml);
		}else{
			//alert("您最多可以加入10条图文！");
			showToast(false,"您最多可以加入10条图文！");
		}
		
		
	}
	
	/**
	 * 删除图文
	 * @param row 要删除的行号
	 * @returns
	 */
	function deleteArticle(row){
		var articleId = 'article_'+row;
		var formArticleId = 'form_article_'+row;
		$("#"+articleId).remove();
		$("#"+formArticleId).remove();
		
		$("#col_2 table[id]").hide();
		$("#form_article_0").show();
	}
	
	/**
	 * 编辑图文
	 * @param row 要编辑的行号
	 * @returns
	 */
	function editArticle(row){
		var count = 0;
		var marginTop = 174;
		$("#col_2 table[id]").hide();
		$("#col_2 table[id]").each(function(i){
			var tableid = this.id;
			tableid = tableid.substr(tableid.lastIndexOf("_")+1);
			if( row == parseInt(tableid) ){
				count = i;
			}
		});
		if(count >= 1){
			marginTop = marginTop + (count - 1) * 80;
		}else{
			marginTop = 0;
		}
		$("#form_article_"+row).css("margin-top", marginTop+"px");
		$("#form_article_"+row).show();
	}
	
	function selectfile(fileId){
		$("#"+fileId).click();
	}
	function uploadPic(fileId){
		var index  = fileId.substr(fileId.indexOf("_")+1);
		
		var url = rootPath+"/material/uploadImage.mge";
		$('#'+fileId).wrap('<form id="FileUpload" action="' + url + '" method="post" enctype="multipart/form-data" target="fileUploadResult"></form>'); 
		$('#FileUpload').submit();
		setTimeout(function(){
			var iframe = $(window.frames["fileUploadResult"].document) ;
			var result = iframe.find("body").html();
			if(result != null && result != ""){
				var data = eval('(' + result + ')');
				if(data.state == "200"){
					$("#pic_"+index).val(data.data);
					$("#img_"+index).attr("src",rootPath+data.data);
					$("#showCover_"+index).show();
					$("#article_img_"+index).attr("src",rootPath+data.data);
					var imgWidth = $("#article_img_"+index).width();
					var imgHeight = $("#article_img_"+index).height();
					if(imgWidth > imgHeight){
						$("#article_img_"+index).attr("width","100%");
					}else{
						$("#article_img_"+index).attr("height","100%");
					}
					$("#article_thumb_"+index).hide();
				}else{
					//alert("上传失败请重新上传2");
					showToast(false,"上传失败请重新上传");
				}
			}else{
				//alert("上传失败请重新上传1");
				showToast(false,"上传失败请重新上传");
			}
			$('#'+fileId).clone().insertAfter("#pic_"+index);
			iframe.find("body").html("");
			$('#FileUpload').remove();
		},1000);
	}
	
	function deletePic(picId){
		var imgUrl = $("#"+picId).val();
		var index = picId.substr(picId.lastIndexOf("_")+1);
		$.ajax({
		  	url: rootPath+"/material/deleteImage.mge?imgUrl="+imgUrl,
		  	type: "DELETE",
		  	dataType: "json",
			data: "imgUrl="+imgUrl,
		  	success: function( data ) {
				if(data.state == "200"){
					$("#pic_"+index).val();
					$("#img_"+index).attr("src","");
					$("#showCover_"+index).hide();
					$("#article_img_"+index).attr("src","");
					$("#article_img_"+index).attr("height","0");
					$("#article_thumb_"+index).show();
				}else{
					//alert(data.msg);
					showToast(false,data.msg);
				}
		  	}
		});
	}
	//编辑标题时左侧模板显示编辑内容
	function showTitle(row){
		var title = $("#title_"+row).val();
		if(title == ""){
			$("#article_title_"+row).html("标题");
		}else{
			$("#article_title_"+row).html(title);
		}
	}
	//鼠标经过显示编辑遮蔽层
	function showEdit(article_edit_id){
		$("#"+article_edit_id).show();
	}
	//鼠标移除后隐藏编辑遮蔽层
	function hideEdit(article_edit_id){
		$("#"+article_edit_id).hide();
	}
	//保存图文消息
	function addManyArticles(){
		var flag = checkForm();
			//showToast(flag);
		if(flag){
			//$("#materialForm").submit();
			$.ajax({
			  	url: rootPath+"/material/add.mge",
			  	type: "POST",
			  	dataType: "json",
				data: $("#materialForm").serialize(),
			  	success: function( data ) {
					if(data.state == "200"){
						showToast(true,data.msg);
					}else{
						showToast(false,data.msg);
					}
			  	}
			});
		}
	}
	//修改图文消息
	function updateManyArticles(){
		var flag = checkForm();
			showToast(flag);
		if(flag){
			//$("#materialForm").submit();
			$.ajax({
			  	url: rootPath+"/material/update.mge",
			  	type: "POST",
			  	dataType: "json",
				data: $("#materialForm").serialize(),
			  	success: function( data ) {
					if(data.state == "200"){
						showToast(true,data.msg);
					}else{
						showToast(false,data.msg);
					}
			  	}
			});
		}
	}
	//验证表单
	function checkForm(){
		var flag = true;
		var count = 0;
		var marginTop = 174;
		var row = 0;
		$("#col_2 table[id]").each(function(i){
			var tableid = this.id;
			tableid = tableid.substr(tableid.lastIndexOf("_")+1);
			var titleId = "hint_title_"+tableid;
			var descriptId = "hint_description_"+tableid;
			var coverId = "hint_cover_"+tableid;
			var urlId = "hint_url_"+tableid;
			if($("#title_"+tableid).val() == "" || $("#title_"+tableid).val().length > 64){
				flag = false;
				count = i;
				row = tableid;
				$("#"+titleId).html("标题不能为空或长度大于64字符");
				$("#title_"+tableid).focus();
			}else{
				$("#"+titleId).html("");
			}
			if($("#description_"+tableid).val() == "" || $("#description_"+tableid).val().length > 120){
				flag = false;
				count = i;
				row = tableid;
				$("#"+descriptId).html("描述不能为空或长度大于120字符");
				$("#description_"+tableid).focus();
			}else{
				$("#"+descriptId).html("");
			}
			if($("#pic_"+tableid).val() == ""){
				flag = false;
				count = i;
				row = tableid;
				$("#"+coverId).html("必须插入一张图片");
				$("#pic_"+tableid).focus();
			}else{
				$("#"+coverId).html("");
			}
			if($("#url_"+tableid).val() == "" || !CheckUrl($("#url_"+tableid).val())){
				flag = false;
				count = i;
				row = tableid;
				$("#"+urlId).html("链接地址不能为空或链接不合法");
				$("#url_"+tableid).focus();
			}else{
				$("#"+urlId).html("");
			}
			if(!flag){
				$("#col_2 table[id]").hide();
				if(count >= 1){
					marginTop = marginTop + (count - 1) * 80;
				}else{
					marginTop = 0;
				}
				$("#form_article_"+row).css("margin-top", marginTop+"px");
				$("#form_article_"+row).show();
				return false;	//终止循环
			}
		});
		return flag;
	}
	$(document).ready(function(){
		 $(".item_img").each(function(i){
			var imgWidth = $(this).width();
			var imgHeight = $(this).height();
			if(imgWidth > imgHeight){
				$(this).attr("width","100%");
			}else{
				$(this).attr("height","100%");
			}
		});
	});
	
	function goToManagePage(appid){
		location.href = rootPath + '/material/manage.mge?appid=' + appid + '&materialType=news';
	}